// pages/addressList/index.js
import {getSetting,chooseAddress,openSetting} from '../../utils/asyncWx.js'
import regeneratorRuntime from '../../lib/runtime/runtime';
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultAddress: null
  },
  onLoad(options){
    this.setData({
      cardId:options.id
    })
  },
  onShow () {
    this.getAddress()
  },
  //获取地址列表
  getAddress(){
    let address= wx.getStorageSync('address')
    if(!address){
      App._get('User/Api001', {}, (res) => {
        let defaultAddress = res.items.find(item=>item.isDefault==1)
        this.setData({defaultAddress})
      })
    }else{
      let defaultAddress ={
        linkName: address.userName,
        linkTel:  address.telNumber,
        provinceTitle:  address.provinceName,
        cityTitle:  address.cityName,
        countyTitle:  address.countyName,
        address:  address.detailInfo
      }
      this.setData({defaultAddress})
    }
  },
  toPay(){
    /* "id": "string",
     "linkName": "string",
     "linkTel": "string",
     "city": "string",
     "address": "string"*/
      console.log()
    let defaultAddress=this.data.defaultAddress
    if(!defaultAddress){
      App.showMsg('请先选择收货地址，在进行支付')
      return false
    }
    let dataForm={
      id:this.data.cardId,
      linkName:defaultAddress.linkName,
      linkTel:defaultAddress.linkTel,
      city:defaultAddress.provinceTitle +' '+defaultAddress.cityTitle +' '+defaultAddress.countyTitle,
      address:defaultAddress.address,
    }
    App._post_form('Card/Api007', dataForm, (res) => {
      console.log(res.data.payStatus==1)
        if(res.data.payStatus==1){
          App.showSuccess(res.msg,()=>{
            wx.navigateTo({
              url: '/pages/memberCard/index?navIndex=1'
            })
          })
        }else{
          App.showMsg(res.msg)
        }
    })
  }
})